const Mock = require('mockjs')

// 扩展：自己生成规则
Mock.Random.extend({
  phone() {
    var phonePrefixs = ['132', '135', '189', '151', '138']
    return this.pick(phonePrefixs) + Mock.mock(/\d{8}/)
  },
  identityCard() {
    return '342622' + Mock.mock(/\d{12}/)
  },
  enterprise() {
    const enterpriseSuffix = '有限公司'
    return Mock.mock('@cword(3,5)') + enterpriseSuffix
  }
})
const List = []
const count = 100
for (let i = 0; i < count; i++) {
  List.push(Mock.mock({
    id: '@increment(1)',
    fullName: '@cname',
    userName: '@word(4,8)',
    telephone: '@phone',
    eMail: '@email("163.com")',
    identityCard: '@identityCard'
  }))
}

const ListOfEnterprise = []
for (let i = 0; i < count; i++) {
  ListOfEnterprise.push(Mock.mock({
    id: '@increment',
    // enterprise: 'djaskld',
    enterprise: '@enterprise',
    // 'enterprise|1': ['立思辰新技术有限公司', '中国信工所', '西电网信院'],
    userName: '@cname',
    contact: '@phone',
    officialName: '@cname',
    officialContact: '@phone'
  }))
}

module.exports = [
  {
    url: '/yunwu-demo/userServiceData/personalUsers/list',
    type: 'get',
    response: config => {
      const { userName, page = 1, limit = 20, sort } = config.query
      let mockList = List.filter(item => {
        if (userName && item.userName.indexOf(userName) < 0) return false
        return true
      })

      if (sort === '-id') {
        mockList = mockList.reverse()
      }

      const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

      return {
        code: 20000,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  },
  {
    url: '/yunwu-demo/userServiceData/personalUsers/detail',
    type: 'get',
    response: config => {
      const { id } = config.query
      for (const article of List) {
        if (article.id === +id) {
          return {
            code: 20000,
            data: article
          }
        }
      }
    }
  },
  {
    url: '/yunwu-demo/userServiceData/personalUsers/create',
    type: 'post',
    response: config => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  },
  {
    url: '/yunwu-demo/userServiceData/personalUsers/update',
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  },
  {
    url: '/yunwu-demo/userServiceData/businessUsers/list',
    type: 'get',
    response: config => {
      const { userName, page = 1, limit = 20, sort } = config.query
      let mockList = ListOfEnterprise.filter(item => {
        if (userName && item.userName.indexOf(userName) < 0) return false
        return true
      })

      if (sort === '-id') {
        mockList = mockList.reverse()
      }

      const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))

      return {
        code: 20000,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  },
  {
    url: '/yunwu-demo/userServiceData/businessUsers/detail',
    type: 'get',
    response: config => {
      const { id } = config.query
      for (const article of ListOfEnterprise) {
        if (article.id === +id) {
          return {
            code: 20000,
            data: article
          }
        }
      }
    }
  },
  {
    url: '/yunwu-demo/userServiceData/businessUsers/create',
    type: 'post',
    response: config => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  },
  {
    url: '/yunwu-demo/userServiceData/businessUsers/update',
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  }
]
